﻿using UnityEngine;
using System.Collections;

public class AmmoBarPointer : MonoBehaviour {
    bool m_bDrag;

    public int m_iObjNum;

    float m_fBarLength;
    float m_fBarPointerMinPos; // 상점구입스크롤 포인터 최소 로컬X 위치
    
	// Use this for initialization
	void Start () {
        m_fBarLength = 18.5f;

        m_fBarPointerMinPos = -3f;

        transform.localPosition = new Vector3(-3 + (transform.parent.GetComponent<AmmoChart>().m_ammoBox.m_iMagazine * ((m_fBarLength / m_iObjNum))), 0);
        BuyingObj_NumCheck();
	}
	
	// Update is called once per frame
	void Update () {
        if (m_bDrag)
        {
            if (Input.GetMouseButton(0))
            {// Drag!
                DragObject();
                BuyingObj_NumCheck();
            }
        }
	}

    void OnPress(bool isDown)
    {
        if (isDown)
            m_bDrag = true;
        else
            m_bDrag = false;
    }

    public void BuyingObj_NumCheck()
    {
        transform.parent.GetComponent<AmmoChart>().m_iNumOfBuyingObj = (int)((transform.localPosition.x - m_fBarPointerMinPos) / (m_fBarLength / m_iObjNum));
    }

    void DragObject()
    {
        transform.position = new Vector3(UICamera.mainCamera.ScreenToWorldPoint(Input.mousePosition).x, transform.position.y);

        if (transform.localPosition.x < -3 + (transform.parent.GetComponent<AmmoChart>().m_ammoBox.m_iMagazine * ((m_fBarLength / m_iObjNum))))
            transform.localPosition = new Vector3( -3 + (transform.parent.GetComponent<AmmoChart>().m_ammoBox.m_iMagazine * ((m_fBarLength / m_iObjNum))), 0);
        else if (transform.localPosition.x > 15.5)
            transform.localPosition = new Vector3(15.5f, 0);
    }
}
